<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
/*$redis->set('test','hellow redis');
echo $redis->get('test');
*/
/*$redis->hSet('article:92617','title','Go to statement considered harmful');
$redis->hSet('article:92617','link','http://ggo.gl/kzuSU');
$redis->hSet('article:92617','poster','user:83271');
$redis->hSet('article:92617','time',time());
$redis->hSet('article:92617','vote','528');
$redis->hSet('article:92617','votes','528');
*/
//$link = $redis->hGet('article:92617','link');
//$redis->hDel('article:92617','vote');
/*$res = $redis->hGetAll('article:92617');
print_r($res);*/


/*
$redis->zAdd('time:','1332065417','article:100408');
$redis->zAdd('time:','1332075503','article:100635');
$redis->zAdd('time:','1332082035','article:100716');


$redis->zAdd('score:','1332164063','article:100635');
$redis->zAdd('score:','1332174713','article:100408');
$redis->zAdd('score:','1332225027','article:100716');*/

//$zrange = $redis->zRange('score:',0,-1,true);
//print_r($zrange);

/*foreach($zrange as $k=>$v){
	$res = $redis->zDelete("score:",$k);
	var_dump($res);
}*/
//die();

/*$redis->sadd('vote:100408','user:234487');
$redis->sAdd('vote:100408','user:253378');
$redis->sAdd('vote:100408','user:364680');
$redis->sAdd('vote:100408','user:132097');
$redis->sAdd('vote:100408','user:350917');*/

//$isHas = $redis->sIsMember('vote:100408','user:132097');
//var_dump($isHas);
//$set = $redis->sMembers("vote:100408");
//print_r($set);

/*$article = "article:92617";
print_R();
die();*/

/*$timeList = $redis->zrange("time:",0,-1,true);
var_dump($timeList);
$voteList = $redis->smembers("vote:100408");
print_R($voteList);*/
define('ONE_WEEK_IN_SECONDS',24*3600*7);
define('VOTE_SCORE',432);
function article_vote($redis,$article,$user)
{
	$one_week_in_seconds = 24*3600*7;
	$vote_score = 432;
	$cutoff = time() - $one_week_in_seconds;

	if($redis->zscore('time:',$article) < $cutoff){
		//return ;
	}
	if(!$redis->zscore('time:',$article)){
		$redis->zadd("time:",time(),$article);
	}
	//die();
	$article_id = explode(':',$article)[1];
	if($redis->sadd("vote:".$article_id,$user)){
	
		$res = $redis->zIncrBy('score:',$vote_score,$article);
	
		$redis->hIncrBy($article,'votes',1);
	}
}

//article_vote($redis,'article:11111',"user:10002");

function post_article($redis,$user,$title,$link)
{
	$article_id = $redis->incr('article:');
	$voted =  'voted:' + $article_id;
	$redis->sadd($voted,$user);
	$redis->expire($voted,ONE_WEEK_IN_SECONDS);
	$time = time();
	$article = 'article:'.$article_id;

	$art = [
		'title'=>$title,
		'link'=>$link,
		'poster'=>$user,
		'time'=>time(),
		'votes'=>1
	];
	$res = $redis->hmset($article,$art);
	$redis->zadd('score:',$time+VOTE_SCORE,$article);
	$redis->zadd('time:',$time,$article);
	return $article_id;
}
//post_article($redis,'user:222','222222','baidu.com');
//$redis->hgetall();

define(ARTICLES_PER_PAGE,25);
function get_articles($redis,$page,$order='score:')
{

	$start = ($page - 1) * ARTICLES_PER_PAGE;
	$end = $start + ARTICLES_PER_PAGE - 1;

	$ids = $redis->zrevrange($order,$start,$end);

	$articles = [];

	foreach($ids as $id){
		$article_data = $redis->hgetall($id);
		$article_data['id'] = $id;
		$articles[] = $article_data;
	}
	return $articles;
}

	
